package com.filldream.fastboot.web.controller.pc;


import com.filldream.fastboot.base.entity.WxConfig;
import com.filldream.fastboot.base.request.IdSTO;
import com.filldream.fastboot.base.request.pc.wxconfig.AddWxConfigSTO;
import com.filldream.fastboot.base.request.pc.wxconfig.ModifyWxConfigSTO;
import com.filldream.fastboot.base.request.pc.wxconfig.WxConfigListSTO;
import com.filldream.fastboot.common.entity.B;
import com.filldream.fastboot.common.entity.P;
import com.filldream.fastboot.common.entity.R;
import com.filldream.fastboot.common.util.TransferUtil;
import com.filldream.fastboot.core.manager.WxConfigManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api(tags = "PC-微信配置")
@RestController
@RequestMapping("/pc/wxConfig")
public class PcWxConfigController {

    @Resource
    private WxConfigManager wxConfigManager;

    @ApiOperation(value = "配置列表")
    @PostMapping("/list")
    public P<WxConfig> list(@RequestBody WxConfigListSTO wxConfigListSTO){
        return wxConfigManager.getPageList(wxConfigListSTO);
    }

    @ApiOperation(value = "添加配置")
    @PostMapping("/add")
    public B add(@RequestBody @Valid AddWxConfigSTO addWxConfigSTO ){
        return B.checkSuccess(wxConfigManager.addWxConfig( TransferUtil.changeToObj(addWxConfigSTO,WxConfig.class) ));
    }

    @ApiOperation(value = "修改配置")
    @PostMapping("/modify")
    public B modify(@RequestBody @Valid ModifyWxConfigSTO modifyWxConfigSTO ){
        Integer insert = wxConfigManager.updateWxConfig(TransferUtil.changeToObj(modifyWxConfigSTO, WxConfig.class));
        return B.checkSuccess(insert);
    }

    @ApiOperation(value = "刷新AccessToken",notes = "增改之后都要刷新一下AccessToken")
    @PostMapping("/refreshToken")
    public R<WxConfig> refreshToken(@RequestBody IdSTO idSTO ){
        return R.autoSuccess(wxConfigManager.refreshAccessToken(idSTO.getId()));
    }



}
